package icu.groot.alan.servlet.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import icu.groot.alan.service.DepartmentService;
import icu.groot.alan.service.UserService;
import icu.groot.alan.service.impl.DepartmentServiceImpl;
import icu.groot.alan.service.impl.UserServiceImpl;
import icu.groot.alan.util.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/deleted.do")
public class UserDeletedServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String codes = req.getParameter("codes");
        ObjectMapper objectMapper = new ObjectMapper();

        if(StringUtils.isAnyBlank(codes)) {
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("请选择需要删除的元素")));
            return;
        }
        UserService userService = new UserServiceImpl();
        R result = userService.deleted(req, resp, codes);
        resp.getWriter().print(objectMapper.writeValueAsString(result));

    }
}
